let time = 0;
/*倒计时*/
function countDown() {
    time = 60;
    $("#securityCodeButton").attr("disabled", true);
    $("#securityCodeButton").attr("class", "get_num_btn");
    setTime();
    function setTime() {
        if (time === 0) {
            $("#securityCodeButton").val("获取邮箱校验码");
            $("#securityCodeButton").attr("disabled", false);
            $("#securityCodeButton").attr("class", "sbmt_btn");
            return false;
        } else {
            $("#securityCodeButton").val(time + "秒后重新发送");
            time--;
        }
        setTimeout(function () {
            setTime(time);
        }, 1000)
    };
}

/*注册*/
function register() {
    let email = $("#email").val();
    let securityCode = $("#securityCode").val();
    let psw = $("#password").val();
    let rePsw = $("#rePassword").val();
    let filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (email === "") {
        alert("邮箱号不能为空");
    } else {
        if (psw === "" || rePsw === "") {
            alert("密码不能为空");
        } else {
            if (!filter.test(email)) {
                alert("邮箱格式错误");
            } else {
                if (psw !== rePsw) {
                    alert("输入密码不一致");
                } else {
                    if (!time > 0 && time <= 60) {
                        alert("校验码失效，请重新获取");
                    } else {
                        $.ajax({
                            headers:{
                                token:$.cookie("token")
                            },
                            url: "http://localhost:8080/tshopping/RegisterController/register",
                            type: 'post',
                            dataType: 'json',
                            data: {email: email, password: psw, securityCode: securityCode},
                            success: function (data) {
                                if (data === true) {
                                    alert("注册成功");
                                    window.location = "login.html";
                                } else {
                                    alert("注册失败");
                                }
                            },
                            error: function (data) {
                                console.log(data);
                                alert("世界上最遥远的距离就是没网");
                            }
                        });
                    }
                }
            }
        }
    }
}

/*发送验证码*/
function sendSecurityCode() {
    let email = $("#email").val();
    let filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (email === "") {
        alert("邮箱号不能为空");
    } else {
        if (!filter.test(email)) {
            alert("邮箱格式错误");
        } else {
            $.ajax({
                headers:{
                    token:$.cookie("token")
                },
                url: "http://localhost:8080/tshopping/RegisterController/sendSecurityCode",
                type: 'post',
                dataType: 'json',
                data: {email: email},
                success: function (data) {
                    countDown();
                },
                error: function (data) {
                    console.log(data);
                    alert("获取验证码失败");
                }
            });
        }
    }
}